package de.stalinski.db;

import de.stalinski.util.Logging;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

public class AccountsConnector
{

    private static String _sUsr = "root";

    private static String _sPwd = "admin";

    private static String _sDB = "frbrUsers";

    private static String _sTable = "accounts";

    private static Connection _connection;

    private static String _sHost = "localhost";

    private static String _sConn = "jdbc:mysql://" + _sHost + ":3306/" + _sDB;

    public AccountsConnector() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch ( ClassNotFoundException ex ) {
            Logging.log(Logging.ERROR, ex, this.getClass().getName());
        }
        try {
            if ( _connection == null ) {
                _connection = DriverManager.getConnection(_sConn, _sUsr, _sPwd);
            }
        } catch ( SQLException ex ) {
            Logging.log(Logging.ERROR, ex, this.getClass().getName());
        }
    }

    public int getAccountID(String account) {
        int result = -1;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            _connection = DriverManager.getConnection(_sConn, _sUsr, _sPwd);
            Statement st = null;
            ResultSet rs = null;
            st = _connection.createStatement();
            rs = st.executeQuery("select * from " + _sTable + " WHERE NAME='" + account + "'");
            while ( rs.next() ) {
                result = rs.getInt("ID");
            }
            _connection.close();
        } catch ( SQLException ex ) {
            Logging.log(Logging.ERROR, ex, this.getClass().getName());
        } catch ( ClassNotFoundException ex ) {
            Logging.log(Logging.ERROR, ex, this.getClass().getName());
        }

        return result;
    }

    public String buildBookNumber() {
        Date now = new Date(System.currentTimeMillis());
        SimpleDateFormat df = new SimpleDateFormat("ddMMyyyy");
        return df.format(now) + "_" + now.getHours() + now.getMinutes() + now.
                getSeconds();
    }

    public String getDate() {
        Date now = new Date(System.currentTimeMillis());
        SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        return df.format(now);
    }

    public String getAccountsNameDropDown(boolean autoSubmit) {
        String out = "";
        try {
            if ( autoSubmit ) {
                out += "<form name=\"acc\" method=\"GET\" action=\"\">";
                out += "<select name=\"account\" onchange='document.acc.submit()'>";
                out += "<option onclick='document.acc.submit()'>Alle</option>";
            } else {
                out += "<select name=\"account\">";
                out += "<option>Alle</option>";
            }

            try {
                Class.forName("com.mysql.jdbc.Driver");
                _connection = DriverManager.getConnection(_sConn, _sUsr, _sPwd);
                Statement st = null;
                ResultSet rs = null;
                st = _connection.createStatement();
                rs = st.executeQuery("select * from " + _sTable);
                while ( rs.next() ) {
                    if ( autoSubmit ) {
                        out += "<option onclick='document.acc.submit()'>" + rs.
                                getString("NAME") + "</option>";
                    } else {
                        out += "<option>" + rs.getString("NAME") + "</option>";
                    }

                }
            } catch ( SQLException ex ) {
                Logging.log(Logging.ERROR, ex, this.getClass().getName());
            } catch ( ClassNotFoundException ex ) {
                Logging.log(Logging.ERROR, ex, this.getClass().getName());
            }
            out += "</select>";
            if ( autoSubmit ) {
                out += "</form>";
            }

            if ( !_connection.isClosed() ) {
                _connection.close();
            }

        } catch ( SQLException ex ) {
            Logging.log(Logging.ERROR, ex, this.getClass().getName());
        }
        return out;
    }

}
